<template>
  <div>
    <el-dialog
      :title="dialogInfo.isAdd ? '添加商品规格' : '编辑商品规格'"
      :visible.sync="dialogInfo.isShow"
      :before-close="cancel"
    >
      <el-form :model="specsInfo" :rules="rules" ref="specsInfo">
        <el-form-item
          label="商品规格名称"
          :label-width="formLabelWidth"
          prop="specsname"
        >
          <el-input v-model="specsInfo.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          v-for="(item, index) in attrArr"
          :label-width="formLabelWidth"
          label="规格属性"
          :key="index"
        >
          <el-input style="width: 75%" v-model="item.value"></el-input>
          <el-button v-if="index == 0" @click="addAttr" type="primary"
            >新增规格属性</el-button
          >
          <el-button v-else type="danger" @click="delAttr(item)"
            >删除</el-button
          >
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="specsInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="dialogInfo.isAdd" type="primary" @click="addSpecs"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="editSpecs">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { getSpecsInfo, getSpecsEdit, postAddSpecs } from "../../util/axios";
export default {
  data() {
    return {
      specsInfo: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      formLabelWidth: "120px",
      rules: {
        specsname: [
          { required: true, message: "请输入商品规格名称", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
      attrArr: [
        {
          value: "",
        },
      ],
    };
  },
  props: ["dialogInfo"],
  methods: {
    ...mapActions({
      getSpecsListAction: "specs/getSpecsListAction",
      getSpecsCountAction: "specs/getSpecsCountAction",
      changePage: "specs/changePageAction",
    }),
    addAttr() {
      if (this.attrArr.length < 6) {
        this.attrArr.push({
          value: "",
        });
      } else {
        this.$message.warning("最多添加6个表单项！！");
      }
    },
    delAttr(item) {
      var index = this.attrArr.indexOf(item);
      if (index !== -1) {
        this.attrArr.splice(index, 1);
      }
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    reset() {
      this.specsInfo = {
        specsname: "",
        attrs: "",
        status: 1,
      };
      this.$refs.specsInfo.resetFields();
      this.attrArr = [
        {
          value: "",
        },
      ];
    },
    addSpecs() {
      this.$refs.specsInfo.validate((valid) => {
        if (valid) {
          // 规格属性的添加
          this.specsInfo.attrs = this.attrArr
            .map((item) => item.value)
            .join(",");
          postAddSpecs(this.specsInfo).then((res) => {
            if (res.data.code == 200) {
              this.specsInfo = res.data.list;
              this.getSpecsListAction();
              this.getSpecsCountAction();
              this.$message.success(res.data.msg);
              this.cancel();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return;
        }
      });
    },
    lookup(id) {
      getSpecsInfo({ id }).then((res) => {
        if (res.data.code == 200) {
          let list = res.data.list[0];
          list.attrs.map((item, idx) => {
            console.log(item, "数组中的每一项");
            if (idx == 0) {
              this.attrArr[0].value = item;
            } else {
              this.attrArr.push({
                value: item,
              });
            }
            this.specsInfo = list;
            this.specsInfo.id = id;
          });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    editSpecs() {
      this.$refs.specsInfo.validate((valid) => {
        if (valid) {
          // 规格属性的添加
          this.specsInfo.attrs = this.attrArr
            .map((item) => item.value)
            .join(",");
          getSpecsEdit(this.specsInfo).then((res) => {
            if (res.data.code == 200) {
              this.specsInfo = res.data.list;
              this.getSpecsListAction();
              this.getSpecsCountAction();
              this.$message.success(res.data.msg);
              this.cancel();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return;
        }
      });
    },
  },
};
</script>

<style lang="" scoped>
</style>
